home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-03-27 | 4.6 KB | 137 lines | [TEXT/ttxt] |
- ; Elementary checkbook–balancer. Keeps track of the number of pennies
- ; in your account, as a 32-bit signed integer, and therefore will fail
- ; whenever your account balance much exceeds 20 million dollars (or when
- ; you are overdrawn by that amount).
- ;
- ; If you have more than 20 million dollars in your checkbook, you
- ; can presumably afford better software. If you are more than 20 million
- ; dollars overdrawn, I doubt that better software will help.
-
-
- ;;; The following symbols will be rebound globally to appropriate
- ;;; functions, when "make-checkbook" is called.
-
- (define c #f) ;; Account for writing a check, eg (c 49.95).
- (define d #f) ;; Account for making a deposit, eg (d 1000).
- (define l #f) ;; Print ledger: (l).
- (define u #f) ;; Undo last "c" or "d": (u).
-
-
- ;;; MAKE-CHECKBOOK Create a checkbook with the given balance.
-
- (define (make-checkbook brought-forward)
- (let*
- ((balance brought-forward)
- (ledger (list (list 'brought-forward #f brought-forward)))
- (debit (lambda (amount) (set! balance (- balance amount))))
- (credit (lambda (amount) (set! balance (+ balance amount))))
- (enter (lambda (transaction)
- (set! ledger (cons transaction ledger))) )
- (remove-last-transaction (lambda ()
- (set! ledger (cdr ledger)) ) )
- (last-transaction(lambda () (car ledger)))
- (make-check-transaction (lambda (amount)
- (list 'check amount balance) ) )
- (make-deposit-transaction (lambda (amount)
- (list 'deposit amount balance) ) )
- )
- (set! c
- (lambda (amount)
- (debit amount)
- (enter (make-check-transaction amount))
- (transaction-string (last-transaction)) ) )
- (set! d (lambda (amount)
- (credit amount)
- (enter (make-deposit-transaction amount))
- (transaction-string (last-transaction)) ) )
- (set! u (lambda ()
- (let ((removed (last-transaction)))
- (remove-last-transaction)
- (set! balance (transaction-balance (last-transaction)))
- (transaction-string removed)
- #t ) ) )
- (set! l (lambda ()
- (display " Check Deposit Balance") (newline)
- (display " ========= ========= =========") (newline)
- (for-each (lambda (transaction)
- (display (transaction-string transaction))
- (newline) )
- (reverse ledger) )
- #t ) )
- ) )
-
- (define transaction-amount cadr)
-
- (define transaction-balance caddr)
-
- (define transaction-type car)
-
- (define (transaction-string transaction)
- (let ((type (car transaction)))
- (cond ((equal? type 'check)
- (string-append
- (dollar->string (transaction-amount transaction))
- " "
- (dollar->string (transaction-balance transaction)) ) )
- ((equal? type 'deposit)
- (string-append
- " "
- (dollar->string (transaction-amount transaction))
- (dollar->string (transaction-balance transaction)) ) )
- ((equal? type 'brought-forward)
- (string-append
- " "
- (dollar->string (transaction-balance transaction)) ) )
- (else
- (error "Unknown transaction type:" type) ) )
- ) )
-
- (define (string-index char string)
- (do ((i 0 (+ i 1))
- (l (string-length string))
- (found? #f) )
- ((or found?
- (>= i l) )
- found? )
- (if (char=? (string-ref string i) char)
- (set! found? i) ) ) )
-
- (define (dollar->string x)
- (let*
- ((sign-string
- (if (positive? x)
- " "
- "-" ) )
- (dollars (floor (abs x)))
- (dollar-string (number->string dollars '(int)))
- (cents (* 100 (- (abs x) dollars)))
- (cent-string (substring (number->string (+ 100.5 cents) '(fix 1)) 1 3))
- (leading-blanks
- (-
- 9
- (+ 1
- (string-length dollar-string)
- 1
- (string-length cent-string)
- )
- ) )
- )
- (if (negative? leading-blanks)
- (error "Number too large:" x) )
- (let ((blank-string (make-string (+ leading-blanks 1) #\space)))
- (string-append
- blank-string
- sign-string
- dollar-string
- "."
- cent-string
- " " ) ) ) )
-
- (define (error message x)
- (begin (newline)
- (display "Error: ")
- (display message)
- (display " ")
- (display x)
- (newline) ) )
-